package com.hyt.it.ogt.ks.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.it.ogt.ks.enums.BizStatusCode;
import com.hyt.it.ogt.ks.model.entity.MobileDeviceParam;
import com.hyt.it.ogt.ks.model.vo.monitor.response.CandidateExamStateVO;
import com.hyt.it.ogt.ks.model.vo.monitor.response.CandidateLoginVO;
import com.hyt.it.ogt.ks.model.vo.monitor.response.CandidateMonitorStartVO;
import com.hyt.it.ogt.ks.model.vo.monitor.response.CandidateValidPhoto;
import com.hyt.it.ogt.ks.model.vo.monitor.response.MonitorSignVO;
import com.hyt.it.ogt.ks.service.IMobileService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

/**
 * userid即examcandidateid
 *
 * @author 刘恒活
 * @since 2020/8/24 10:12
 */
@RestController
@RequestMapping("/ks/mobile/monitor")
@Api(tags = "5.移动监控接口", value = "移动监控接口")
@ApiSort(5)
@Slf4j
public class MobileMonitorController {
    @Resource
    private IMobileService iMobileService;

    @ApiOperation(value = "5.1.1 移动端登录", notes = "状态码：20000:成功，42050201：异常", response = CandidateLoginVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "admissionNumber", value = "准考证", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "examCode", value = "考试ID", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true)
    })
    @ApiOperationSort(1)
    @GetMapping("login")
    public ApiResponse login(@RequestParam String admissionNumber, @RequestParam String examCode) {
        try {
            return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(iMobileService.login(admissionNumber, examCode,null)).build();
        } catch (Exception e) {
            log.error("移动端登陆异常:{}", e);
            return ApiResponse.builder().code(BizStatusCode.VIDEO_MONITOR_MOBILE_LOGIN_FAIL_CODE.getCode())
                    .data(CandidateLoginVO.builder().status(6).msg("登录失败")).build();
        }
    }

    @ApiOperation(value = "5.1.2 获取签名", notes = "状态码：20000:成功，16323：考生不存在，42050101：异常", response = MonitorSignVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true)
    })
    @ApiOperationSort(2)
    @GetMapping("getSign")
    public ApiResponse getSign(String examId, String userId) {
        try {
            return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(iMobileService.getSign(examId, userId)).build();
        } catch (Exception e) {
            log.error("获取签名报错:{}", e);
            return ApiResponse.builder().code(BizStatusCode.VIDEO_MONITOR_GET_SIGN_FAIL_CODE.getCode()).build();
        }
    }

    @ApiOperation(value = "5.1.3 获取考生照片列表", notes = "状态码：20000:成功，42050201：异常", response = CandidateValidPhoto.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true)
    })
    @ApiOperationSort(3)
    @GetMapping("getCandidateInfo")
    public ApiResponse getCandidateInfo(@RequestParam String userId) {
        try {
            return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(iMobileService.getCandidateInfo(userId)).build();
        } catch (Exception e) {
            log.error("移动端登陆异常:{}", e);
            return ApiResponse.builder().code(BizStatusCode.VIDEO_MONITOR_MOBILE_LOGIN_FAIL_CODE.getCode()).build();
        }
    }

    @ApiOperation(value = "5.1.4 配对分析（人脸比对），返回值true表示验证通过，false表示验证不通过", notes = "状态码：20000:成功，42050401：异常", response =
            Boolean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "imageUrlA", value = "第一张照片(后端返回的照片)", dataType = DataType.STRING,
                    paramType = ParamType.QUERY),
            @ApiImplicitParam(name = "imageUrlB", value = "第二张照片(后端返回的照片)", dataType = DataType.STRING,
                    paramType = ParamType.QUERY),
            @ApiImplicitParam(name = "imageBase64", value = "第三张照片base64码(移动端拍照照片)", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true)
    })
    @ApiOperationSort(4)
    @PutMapping("faceMatch")
    public ApiResponse faceMatch(@RequestParam String examId, @RequestParam String userId, String imageUrlA,
                                  String imageUrlB, @RequestParam String imageBase64) {
        try {
            return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(iMobileService.faceMatch(examId, userId, imageUrlA, imageUrlB, imageBase64)).build();
        } catch (Exception e) {
            log.error("移动端配对分析异常:{}", e);
            return ApiResponse.builder().code(BizStatusCode.VIDEO_MONITOR_COMPARE_FACE_FAIL_CODE.getCode()).build();
        }
    }

    @ApiOperation(value = "5.1.5 开始监控", notes = "状态码：20000:成功，42051501：异常", response = CandidateMonitorStartVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "examId", value = "examId", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true)
    })
    @ApiOperationSort(5)
    @GetMapping("startMonitor")
    public ApiResponse startMonitor(@RequestParam String userId, @RequestParam String examId) {
        try {
            return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(iMobileService.startMonitor(userId, examId)).build();
        } catch (Exception e) {
            log.error("移动端开始监控异常:{}", e);
            return ApiResponse.builder().code(BizStatusCode.VIDEO_MONITOR_START_MONITOR_ERROR_CODE.getCode()).build();
        }
    }

    @ApiOperation(value = "5.1.6 查看考生考试状态", notes = "状态码：20000:成功，18326：异常", response = CandidateExamStateVO.class)
    @ApiOperationSort(6)
    @GetMapping("getMonitorState")
    public ApiResponse getMonitorState(MobileDeviceParam param) {
        try {
            return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(iMobileService.getMonitorState(param)).build();
        } catch (Exception e) {
            log.error("移动端开始监控异常:{}，{}", JSON.toJSONString(param), e);
            return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(CandidateExamStateVO.builder().state(0).build()).build();
        }
    }

    @ApiOperation(value = "5.1.7 获取第二监控状态", notes = "状态码：20000:成功", response = CandidateExamStateVO.class)
    @ApiOperationSort(6)
    @GetMapping("getAppMonitorStatus")
    public ApiResponse<CandidateExamStateVO> getAppMonitorStatus(@RequestParam String candidateId, @RequestParam String examId) {
        try {
            return ApiResponse.<CandidateExamStateVO>builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(iMobileService.getAppMonitorStatus(examId,candidateId)).build();
        } catch (Exception e) {
            log.error("移动端开始监控异常:{}，{}", candidateId,examId, e);
            return ApiResponse.<CandidateExamStateVO>builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(CandidateExamStateVO.builder().state(0).build()).build();
        }
    }
}
